<?php

namespace App\Controller\Api\Consultant;

use App\Constants\ErrorCode;
use App\Controller\Api\BaseController;
use App\Helper\HashHelper;
use App\Model\BalanceType;
use App\Model\BalanceTypeSuffixTran;
use App\Model\BalanceTypeTran;
use App\Model\ClassHour;
use App\Model\ConfusionCategory;
use App\Model\ConsultantBanner;
use App\Model\ConsultantContactType;
use App\Model\ConsultantCrowdTran;
use App\Model\ConsultantGenreTran;
use App\Model\ConsultantStyleTran;
use App\Model\Country;
use App\Model\EducationLevel;
use App\Model\Language;
use App\Model\NoticeCarousel;
use App\Model\Policy;
use App\Model\ReportType;
use App\Model\User;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;
use Psr\SimpleCache\InvalidArgumentException;
use RedisException;


/**
 * @Controller(prefix="/consultant/v1/common")
 */
class CommonController extends BaseController
{

    /**
     * @RequestMapping(path="initInfo")
     * @return ResponseInterface
     */
    public function initInfo()
    {

        $md5Kkey = env('MD5_KEY', 'Nbef8NfqWE4embWO');

        $timestamp = $this->request->header('timestamp', '');


        $sign = $this->request->query('sign');

        $checkSign = md5($md5Kkey . $timestamp);


        if ($sign != $checkSign) {
            return $this->response->returnFail();
        }


        $policy = Policy::getPolicy();
        $data['policy'] = $policy;

        $data['hx'] = [
            'appKey' => env('HX_APP_KEY', '1171230214166361#seekingdev'),
            'clientId' => env('HX_CLIENT_ID', 'YXA6pLiJODEASniMD6Bu1X8NfA'),
            'prefix' => 'zx'
        ];

        $data['version'] = [
            'versionId' => 1,
            'version' => '1.0.0'
        ];

        $data['downloadUrl'] = 'https://www.badiu.com';


        $data['share'] = [
            'url' => 'https://www.baidu.com',
            'title' => '分享标题',
            'message' => 'message'
        ];
        $data['agora'] = [
            'appId' =>env('AGORA_APPID','d00be774b41b4ab7a12dd2be282e5f44'),
            'appCertificate' =>env('AGORA_CERTIFICATE','036fbee15812471084b0e63d72c1104b'),
        ];



        return $this->response->returnSuccess($data);

    }


    /**
     * 发送验证码
     * @RequestMapping(path="getVerifyCode")
     * @return ResponseInterface
     */
    public function getVerifyCode()
    {

        $params = $this->request->post();

        $this->validate($params, [
            'type' => 'required',
            'account' => 'required',
        ]);


        return $this->response->returnSuccess();


    }


    /**
     * 国家列表
     * 缓存--countryList
     * @RequestMapping(path="countryList")
     */
    public function countryList()
    {

        $countryList = cache()->get('countryList');

        if (is_null($countryList)) {

            $countryList = Country::where(['pid' => 0])->select(['id', 'self_name as name', 'code', 'code3'])->get()->toArray();
            cache()->set('countryList', $countryList);

        }

        return $this->response->returnSuccess(['list' => $countryList]);

    }


    /**
     * 语言列表
     * 缓存--languageList
     * @RequestMapping(path="languageList")
     * @return ResponseInterface
     * @throws InvalidArgumentException
     */
    public function languageList()
    {

        $languageList = cache()->get('languageList');

        if (is_null($languageList)) {
            $languageList = Language::get()->toArray();
            cache()->set('languageList', $languageList);
        }

        return $this->response->returnSuccess(['list' => $languageList]);

    }


    /**
     * @RequestMapping(path="classTypeList")
     * @return ResponseInterface
     */
    public function classTypeList()
    {

        $list = ClassHour::orderByDesc('id')->get()->toArray();

        return $this->response->returnSuccess(['list' => $list]);


    }


    /**
     * @RequestMapping(path="optionList")
     * @return ResponseInterface
     * @throws InvalidArgumentException
     */
    public function optionList()
    {
        $lang = getLang();


        $cacheKey = 'confusionCategoryList:' . $lang;


        $confusionCategoryList = cache()->get($cacheKey);
        if (is_null($confusionCategoryList)) {
            $confusionCategoryList = ConfusionCategory::where(['pid' => 0, 'status' => 1, 'lang' => $lang])->get()->toArray();
            if (!empty($confusionCategoryList)) {
                cache()->set($cacheKey, $confusionCategoryList);
            }
        }
        $data['confusionCategoryList'] = $confusionCategoryList;


        $select = [
            'id',
            'name_' . $lang . ' as name'
        ];
        $educationList = EducationLevel::select($select)->get()->toArray();

        $data['educationList'] = $educationList;


        $consultantCrowdListCacheKey = 'consultantCrowdList:' . $lang;


        $consultantCrowdList = cache()->get($consultantCrowdListCacheKey);
        if (is_null($consultantCrowdList)) {
            $consultantCrowdList = ConsultantCrowdTran::select(['consultant_crowd_id as id', 'name'])->orderByDesc('sort')->get()->toArray();
            if (!empty($consultantCrowdList)) {
                cache()->set($consultantCrowdListCacheKey, $consultantCrowdList);
            }
        }
        $data['consultantCrowdList'] = $consultantCrowdList;


        $consultantStyleListCacheKey = 'confusionStyleList:' . $lang;
        $confusionStyleList = cache()->get($consultantStyleListCacheKey);
        if (is_null($confusionStyleList)) {
            $confusionStyleList = ConsultantStyleTran::select(['consultant_style_id as id', 'name'])->orderByDesc('sort')->get()->toArray();
            if (!empty($confusionStyleList)) {
                cache()->set($consultantStyleListCacheKey, $confusionStyleList);
            }
        }
        $data['confusionStyleList'] = $confusionStyleList;


        $consultantGenreListCacheKey = 'consultantGenreList' . $lang;

        $consultantGenreList = cache()->get($consultantGenreListCacheKey);

        if (is_null($consultantGenreList)) {
            $consultantGenreList = ConsultantGenreTran::select(['consultant_genre_id as id', 'name'])->orderByDesc('sort')->get()->toArray();
            if (!empty($consultantGenreList)) {
                cache()->set($consultantGenreListCacheKey, $consultantGenreList);
            }
        }

        $data['consultantGenreList'] = $consultantGenreList;


        $consultantContactTypeListCacheKey = 'consultantContactTypeList';


        $consultantContactTypeList = cache()->get($consultantContactTypeListCacheKey);
        if (is_null($consultantContactTypeList)) {
            $consultantContactTypeList = ConsultantContactType::select(['id', 'name', 'logo'])->orderByDesc('sort')->get()->toArray();
            if (!empty($consultantContactTypeList)) {
                cache()->set($consultantContactTypeListCacheKey, $consultantContactTypeList);
            }
        }
        $data['consultantContactTypeList'] = $consultantContactTypeList;


        return $this->response->returnSuccess($data);


    }


    /**
     * @RequestMapping(path="educationList")
     * @return ResponseInterface
     */
    public function educationList()
    {

        $lang = getLang();


        $select = [
            'id',
            'name_' . $lang . ' as name'
        ];
        $list = EducationLevel::select($select)->get()->toArray();

        return $this->response->returnSuccess(['list' => $list]);


    }


    /**
     * 困惑列表
     * 缓存--confusionCategoryList:
     * @RequestMapping(path="confusionCategoryList")
     * @return ResponseInterface
     * @throws InvalidArgumentException
     */
    public function confusionCategoryList()
    {

        $lang = getLang();
        $cacheKey = 'confusionCategoryList:' . $lang;


        $confusionCategoryList = cache()->get($cacheKey);
        if (is_null($confusionCategoryList)) {
            $confusionCategoryList = ConfusionCategory::where(['pid' => 0, 'status' => 1, 'lang' => $lang])->get()->toArray();
            if (!empty($confusionCategoryList)) {
                cache()->set($cacheKey, $confusionCategoryList);
            }
        }
        return $this->response->returnSuccess(['list' => $confusionCategoryList]);

    }


    /**
     * @RequestMapping(path="consultantCrowdList")
     * @return ResponseInterface
     * @throws InvalidArgumentException
     */
    public function consultantCrowdList()
    {


        $lang = getLang();
        $cacheKey = 'consultantCrowdList:' . $lang;


        $confusionCategoryList = cache()->get($cacheKey);
        if (is_null($confusionCategoryList)) {
            $confusionCategoryList = ConsultantCrowdTran::orderByDesc('sort')->get()->toArray();
            if (!empty($confusionCategoryList)) {
                cache()->set($cacheKey, $confusionCategoryList);
            }
        }
        return $this->response->returnSuccess(['list' => $confusionCategoryList]);


    }


    /**
     * @RequestMapping(path="consultantContactTypeList")
     * @return ResponseInterface
     * @throws InvalidArgumentException
     */
    public function consultantContactTypeList()
    {


        $cacheKey = 'consultantContactTypeList';


        $consultantContactTypeList = cache()->get($cacheKey);
        if (is_null($consultantContactTypeList)) {
            $consultantContactTypeList = ConsultantContactType::orderByDesc('sort')->get()->toArray();
            if (!empty($consultantContactTypeList)) {
                cache()->set($cacheKey, $consultantContactTypeList);
            }
        }
        return $this->response->returnSuccess(['list' => $consultantContactTypeList]);


    }

    /**
     * @RequestMapping(path="resetPassword")
     * @return ResponseInterface
     * @throws RedisException
     */
    public function resetPassword()
    {
        $params = $this->request->post();

        $this->validate($params, [
            'account' => 'required',
            'type' => 'required',
            'code' => 'required',
            'password' => 'required'
        ]);

        $rel = $this->checkBindCode($params['account'], $params['code']);

        if (!$rel) {
            return $this->response->returnFail(ErrorCode::REGISTER_CODE_FAIL);
        }

        $info = User::where([$params['type'] => $params['account']])->first();

        if (is_null($info)) {
            return $this->response->returnFail(ErrorCode::LOGIN_USER_NULL);
        }


        $newPassword = HashHelper::make($params['password']);

        $rel = User::where(['id' =>$info['id']])->update(['password' => $newPassword, 'update_time' => time()]);


        if ($rel) {

            redis()->del('consultantUser:userToken:' . $info['id'], );
            return $this->response->returnSuccess();
        } else {
            return $this->response->returnFail();
        }


    }

    public function checkBindCode($account, $code)
    {


        return true;

    }


    /**
     * @RequestMapping(path="consultantIndexBanner")
     * @return ResponseInterface
     * @throws InvalidArgumentException
     */
    public function consultantIndexBanner(){


        $lang = getLang();
        $cacheKey = 'consultantIndexBanner:' . $lang;

        $consultantIndexBanner = cache()->get($cacheKey);
        if (is_null($consultantIndexBanner)) {
            $consultantIndexBanner = ConsultantBanner::where(['status' => 1,'lang'=>$lang])->orderBy('sort', 'desc')->get()->toArray();

            if (!empty($consultantIndexBanner)) {
                cache()->set($cacheKey, $consultantIndexBanner);
            }
        }

        return $this->response->returnSuccess(['list' => $consultantIndexBanner]);

    }


    /**
     * @RequestMapping(path="noticeCarousel")
     * @return ResponseInterface
     */
    public function noticeCarousel(){
        $lang = getLang();

        $consultantIndexBanner = NoticeCarousel::where(['status' => 1,'lang'=>$lang])->orderBy('sort', 'desc')->get()->toArray();
        return $this->response->returnSuccess(['list' => $consultantIndexBanner]);

    }


    /**
     * 余额账变主类型
     * @RequestMapping(path="balanceTypeList")
     * @return ResponseInterface
     */
    public function balanceTypeList(){

        $lang = getLang();

        $list = BalanceTypeTran::from('balance_type_trans as a')
            ->leftJoin('balance_type as b','a.balance_type_id','=','b.id')
            ->select(['a.balance_type_id as id','a.name','b.url'])
            ->where(['a.lang'=>$lang])
            ->orderBy('a.sort','desc')
            ->get()->toArray();




        return $this->response->returnSuccess(['list' => $list]);
    }


    /**
     * 账变后缀类型
     * @RequestMapping(path="balanceTypeListSuffix")
     * @return ResponseInterface
     */
    public function balanceTypeListSuffix(){

        $lang = getLang();

        $list = BalanceTypeSuffixTran::where(['lang'=>$lang])->orderByDesc('sort')->select(['balance_type_suffix_id as id','name'])->get()->toArray();


        return $this->response->returnSuccess(['list' => $list]);

    }

    /**
     * 提现类型
     * @RequestMapping(path="withdrawalType")
     * @return ResponseInterface
     */
    public function withdrawalType(){

        $lang = getLang();

        $list = BalanceTypeTran::from('withdrawal_type_trans as a')
            ->leftJoin('withdrawal_type as b','a.withdrawal_type_id','=','b.id')
            ->select(['a.withdrawal_type_id as id','a.name','b.url'])
            ->where(['a.lang'=>$lang])
            ->orderBy('a.sort','desc')
            ->get()->toArray();

        $data = [
          'ruleUrl'=>'https://www.baidu.com',
          'policyUrl'=>'https://www.baidu.com',
        ];


        return $this->response->returnSuccess(['policy'=>$data,'list' => $list]);

    }


    /**
     * @RequestMapping(path="reportTypeList")
     * @return ResponseInterface
     * @throws InvalidArgumentException
     */
    public function reportTypeList()
    {


        $type = $this->request->query('type', 1);

        $lang = getLang();
        $cacheKey = 'reportTypeList:' . $type . ':' . $lang;

        $reportTypeList = cache()->get($cacheKey);
        if (is_null($reportTypeList)) {
            $reportTypeList = ReportType::where(['lang' => $lang])->orderByDesc('sort')->get()->toArray();
            if (!empty($reportTypeList)) {
                cache()->set($cacheKey, $reportTypeList);
            } else {
                $reportTypeList = null;
            }
        }

        return $this->response->returnSuccess(['list' => $reportTypeList]);


    }




}